package design3;

/**
 * A calculator used to calculate the square of numbers.
 */
public class SquareCalculator {
	
	private Cache<Number, Number> numberToSquare = new Cache<>();
	
	/**
	 * Gets the square of the given number.
	 * 
	 * @param number
	 *    The number, for which to get the square.
	 * @return
	 *    The square of the given number.
	 */
	public Number getSquare(Number number) {
		if (number == null) {
			throw new IllegalArgumentException("number should not be null.");
		}
		
		Number square = numberToSquare.get(number);
		if (square == null) {
			square = Math.pow(number.doubleValue(), 2);
			numberToSquare.put(number, square);
		}
		
		return square;
	}

}
